﻿package morn.core.components
{
    import flash.events.*;
    import flash.geom.*;
    import morn.core.handlers.*;

    public class Slider extends Component implements ISkinAbleContainer
    {
        protected var _allowBackClick:Boolean;
        protected var _max:Number = 100;
        protected var _min:Number = 0;
        protected var _tick:Number = 1;
        protected var _value:Number = 0;
        protected var _direction:String = "vertical";
        protected var _skin:String;
        protected var _back:Image;
        protected var _bar:Button;
        protected var _label:Label;
        protected var _showLabel:Boolean = true;
        protected var _changeHandler:Handler;
        private var _skinChanged:Boolean = false;
        public static const HORIZONTAL:String = "horizontal";
        public static const VERTICAL:String = "vertical";

        public function Slider(param1:String = null) : void
        {
            if (param1 != null)
            {
                this.skin = param1;
            }
            return;
        }// end function

        public function setSkin(param1:Object) : void
        {
            if (!this._skinChanged)
            {
                this.skin = param1 as String;
                if (this._back.bitmapData)
                {
                    _contentWidth = this._back.bitmapData.width;
                    _contentHeight = this._back.bitmapData.height;
                }
                else
                {
                    _contentWidth = this._back.width;
                    _contentHeight = this._back.height;
                }
                this._width = _contentWidth;
                this._height = _contentHeight;
                this.changeSize();
            }
            return;
        }// end function

        public function set skinChanged(param1:Boolean) : void
        {
            this._skinChanged = param1;
            return;
        }// end function

        override protected function preinitialize() : void
        {
            mouseChildren = true;
            return;
        }// end function

        override protected function createChildren() : void
        {
            var _loc_1:* = new Image();
            this._back = new Image();
            addChild(_loc_1);
            var _loc_1:* = new Button();
            this._bar = new Button();
            addChild(_loc_1);
            var _loc_1:* = new Label();
            this._label = new Label();
            addChild(_loc_1);
            return;
        }// end function

        override protected function initialize() : void
        {
            this._bar.addEventListener(MouseEvent.MOUSE_DOWN, this.onButtonMouseDown);
            var _loc_1:String = "4,4,4,4";
            this._bar.sizeGrid = "4,4,4,4";
            this._back.sizeGrid = _loc_1;
            this.allowBackClick = true;
            return;
        }// end function

        protected function onButtonMouseDown(event:MouseEvent) : void
        {
            App.stage.addEventListener(MouseEvent.MOUSE_UP, this.onStageMouseUp);
            App.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.onStageMouseMove);
            if (this._direction == VERTICAL)
            {
                this._bar.startDrag(false, new Rectangle(this._bar.x, 0, 0, height - this._bar.height));
            }
            else
            {
                this._bar.startDrag(false, new Rectangle(0, this._bar.y, width - this._bar.width, 0));
            }
            this.showValueText();
            return;
        }// end function

        protected function showValueText() : void
        {
            if (this._showLabel)
            {
                this._label.text = this._value + "";
                if (this._direction == VERTICAL)
                {
                    this._label.x = this._bar.x + 20;
                    this._label.y = (this._bar.height - this._label.height) * 0.5 + this._bar.y;
                }
                else
                {
                    this._label.y = this._bar.y - 20;
                    this._label.x = (this._bar.width - this._label.width) * 0.5 + this._bar.x;
                }
            }
            return;
        }// end function

        protected function hideValueText() : void
        {
            this._label.text = "";
            return;
        }// end function

        protected function onStageMouseUp(event:MouseEvent) : void
        {
            App.stage.removeEventListener(MouseEvent.MOUSE_UP, this.onStageMouseUp);
            App.stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.onStageMouseMove);
            this._bar.stopDrag();
            this.hideValueText();
            return;
        }// end function

        protected function onStageMouseMove(event:MouseEvent) : void
        {
            var _loc_2:* = this._value;
            if (this._direction == VERTICAL)
            {
                this._value = this._bar.y / (height - this._bar.height) * (this._max - this._min) + this._min;
            }
            else
            {
                this._value = this._bar.x / (width - this._bar.width) * (this._max - this._min) + this._min;
            }
            this._value = Math.round(this._value / this._tick) * this._tick;
            if (this._value != _loc_2)
            {
                this.showValueText();
                this.sendChangeEvent();
            }
            return;
        }// end function

        protected function sendChangeEvent() : void
        {
            sendEvent(Event.CHANGE);
            if (this._changeHandler != null)
            {
                this._changeHandler.executeWith([this._value]);
            }
            return;
        }// end function

        public function get skin() : String
        {
            return this._skin;
        }// end function

        public function set skin(param1:String) : void
        {
            if (this._skin != param1)
            {
                this._skin = param1;
                this._back.url = this._skin;
                this._bar.skin = this._skin + "$bar";
                if (this._back.bitmap)
                {
                    _contentWidth = this._back.bitmap.width;
                    _contentHeight = this._back.bitmap.height;
                }
                else
                {
                    _contentWidth = this._back.width;
                    _contentHeight = this._back.height;
                }
                this.setBarPoint();
            }
            return;
        }// end function

        override protected function changeSize() : void
        {
            super.changeSize();
            this._back.width = width;
            this._back.height = height;
            this.setBarPoint();
            return;
        }// end function

        protected function setBarPoint() : void
        {
            if (this._direction == VERTICAL)
            {
                this._bar.x = (this._back.width - this._bar.width) * 0.5;
            }
            else
            {
                this._bar.y = (this._back.height - this._bar.height) * 0.5;
            }
            return;
        }// end function

        public function get sizeGrid() : String
        {
            return this._back.sizeGrid;
        }// end function

        public function set sizeGrid(param1:String) : void
        {
            this._back.sizeGrid = param1;
            return;
        }// end function

        protected function changeValue() : void
        {
            this._value = Math.round(this._value / this._tick) * this._tick;
            this._value = this._value > this._max ? (this._max) : (this._value < this._min ? (this._min) : (this._value));
            if (this._direction == VERTICAL)
            {
                this._bar.y = (this._value - this._min) / (this._max - this._min) * (height - this._bar.height);
            }
            else
            {
                this._bar.x = (this._value - this._min) / (this._max - this._min) * (width - this._bar.width);
            }
            return;
        }// end function

        public function setSlider(param1:Number, param2:Number, param3:Number) : void
        {
            this._value = -1;
            this._min = param1;
            this._max = param2 > param1 ? (param2) : (param1);
            this.value = param3 < param1 ? (param1) : (param3 > param2 ? (param2) : (param3));
            return;
        }// end function

        public function get tick() : Number
        {
            return this._tick;
        }// end function

        public function set tick(param1:Number) : void
        {
            this._tick = param1;
            callLater(this.changeValue);
            return;
        }// end function

        public function get max() : Number
        {
            return this._max;
        }// end function

        public function set max(param1:Number) : void
        {
            if (this._max != param1)
            {
                this._max = param1;
                callLater(this.changeValue);
            }
            return;
        }// end function

        public function get min() : Number
        {
            return this._min;
        }// end function

        public function set min(param1:Number) : void
        {
            if (this._min != param1)
            {
                this._min = param1;
                callLater(this.changeValue);
            }
            return;
        }// end function

        public function get value() : Number
        {
            return this._value;
        }// end function

        public function set value(param1:Number) : void
        {
            if (this._value != param1)
            {
                this._value = param1;
                this.changeValue();
                this.sendChangeEvent();
            }
            return;
        }// end function

        public function get direction() : String
        {
            return this._direction;
        }// end function

        public function set direction(param1:String) : void
        {
            this._direction = param1;
            return;
        }// end function

        public function get showLabel() : Boolean
        {
            return this._showLabel;
        }// end function

        public function set showLabel(param1:Boolean) : void
        {
            this._showLabel = param1;
            return;
        }// end function

        public function get allowBackClick() : Boolean
        {
            return this._allowBackClick;
        }// end function

        public function set allowBackClick(param1:Boolean) : void
        {
            if (this._allowBackClick != param1)
            {
                this._allowBackClick = param1;
                if (this._allowBackClick)
                {
                    this._back.addEventListener(MouseEvent.MOUSE_DOWN, this.onBackBoxMouseDown);
                }
                else
                {
                    this._back.removeEventListener(MouseEvent.MOUSE_DOWN, this.onBackBoxMouseDown);
                }
            }
            return;
        }// end function

        protected function onBackBoxMouseDown(event:MouseEvent) : void
        {
            if (this._direction == VERTICAL)
            {
                this.value = this._back.mouseY / (height - this._bar.height) * (this._max - this._min) + this._min;
            }
            else
            {
                this.value = this._back.mouseX / (width - this._bar.width) * (this._max - this._min) + this._min;
            }
            return;
        }// end function

        override public function set dataSource(param1:Object) : void
        {
            _dataSource = param1;
            if (param1 is Number || param1 is String)
            {
                this.value = Number(param1);
            }
            else
            {
                super.dataSource = param1;
            }
            return;
        }// end function

        public function get bar() : Button
        {
            return this._bar;
        }// end function

        public function get changeHandler() : Handler
        {
            return this._changeHandler;
        }// end function

        public function set changeHandler(param1:Handler) : void
        {
            this._changeHandler = param1;
            return;
        }// end function

    }
}
